﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 晨曦超市管理系统.收款
{
    public partial class ReceivablesForm : Form
    {
        public ReceivablesForm()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }
        SqlDataReader root;
        private void ReceivablesForm_Load(object sender, EventArgs e)
        {
            
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label47_Click(object sender, EventArgs e)
        {
            this.label47.Text = string.Format("{0}", DateTime.Now.ToShortDateString());
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void panel12_Paint(object sender, PaintEventArgs e)
        {

        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        //判断我表里的商品
        SqlDataReader dr;
        private void button1_Click(object sender, EventArgs e)
        {
            bool dui = true;//钩子变量
                    string abc = "";
            //判断我表里现在有没有一样的商品，有的话就在原来的商品数量加1金额也变动。把钩子变量改成fales就不会执行添加商品代码。
                    for (int j = 0; j < listView1.Items.Count; j++)
                    {
                        abc = listView1.Items[j].SubItems[0].Text;//遍历
                        if (textBox1.Text.Trim() == abc)
                        {
                            string sqlstr = "select UnitPrice*Discount from CommodityList where ProductID='" + abc + "'";//查询我商品的价格。
                            dr = DBHelper.DateReader(sqlstr);
                            double zhao2 = 0;
                            while (dr.Read())
                            {
                                zhao2 = Convert.ToDouble(dr[0].ToString());
                            }
                            dr.Close();
                            int zhao = Convert.ToInt32(listView1.Items[j].SubItems[6].Text);//获取我表里的数量加1
                            zhao++;
                            listView1.Items[j].SubItems[6].Text = zhao.ToString();
                            double zhongqian = zhao2 * zhao;
                            listView1.Items[j].SubItems[7].Text = zhongqian.ToString();
                            dui = false;
                            break;
                        }
                    }
                    if (dui)
                    {
                        string sqlstr = string.Format("select ProductID,Name1,UnitOfMeasurement,UnitPrice,Discount,UnitPrice*Discount,Shuliang from CommodityList where ProductID='{0}'", textBox1.Text.Trim());
                        root = DBHelper.DateReader(sqlstr);

                        while (root.Read())                                     //root是我定义的值，后面的read是找root里面的值找到一行执行一行，他是bool类型。
                        {
                            double shoujia = Convert.ToDouble(root[5]);
                            ListViewItem item = new ListViewItem(root[0].ToString());  //root【】0就是代表第几列，也可以使用数据库里的列表名字做索引值。他返回的是一个object对象，我要转为tostring（）。
                            item.SubItems.Add(root[1].ToString());
                            item.SubItems.Add(root[2].ToString());
                            item.SubItems.Add(root[3].ToString());
                            item.SubItems.Add(root[4].ToString());
                            item.SubItems.Add(root[5].ToString());
                            item.SubItems.Add("1");
                            item.SubItems.Add(root[5].ToString());
                            item.SubItems.Add(root[6].ToString());
                            listView1.Items.Add(item);
                        }
                        root.Close();                                            //关闭我定义值的连接，然后在关闭数据库的连接
                    }
        }

        private void ReceivablesForm_KeyDown(object sender, KeyEventArgs e)
        {
            //结算
            if (e.KeyCode == Keys.F12)
            {
                try
                {
                    string dan = DateTime.Now.ToString("yyyyMMddHHMMss");
                    string memberName = "", UserNameID = "";
                    for (int i = 0; i < listView1.Items.Count; i++)
                    {
                        string sqlstr = string.Format("insect into Sales(Salesman_Name,UserNameID,memberName)values('{0}','{1}','{2}')", dan, UserNameID, memberName);
                        DBHelper.QEX(sqlstr);
                        string ProductID = listView1.Items[i].SubItems[0].Text;
                        string Quantity = listView1.Items[i].SubItems[6].Text;
                        string AloneAmount = listView1.Items[i].SubItems[7].Text;
                        string sqlstr1 = string.Format("insect into SalesDetail(Salesman_Name,ProductID,Quantity,AloneAmount) values('{0}','{1}','{2}','{3}')", dan, ProductID, Quantity, AloneAmount);
                        DBHelper.QEX(sqlstr1);
                    }
                    listView1.Items.Clear();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            //数量的
            if (true)
            {
                //数量的
                int a = -1;
                if (listView1.SelectedIndices != null && listView1.SelectedIndices.Count > 0)
                {
                    a = int.Parse(listView1.SelectedItems[0].SubItems[6].Text);
                    --a;
                    double b = double.Parse(listView1.SelectedItems[0].SubItems[7].Text);
                    listView1.SelectedItems[0].SubItems[6].Text = a.ToString();
                    double c = a * b;
                    listView1.SelectedItems[0].SubItems[7].Text = c.ToString();
                }
                if (a == 0)
                {
                    listView1.SelectedItems[0].Remove();
                }
                if (listView1.SelectedIndices == null || listView1.SelectedIndices.Count < 0)
                {
                    MessageBox.Show("亲你没有选中行");
                }
            }
            //删除这一行
            if (true)
            {
                listView1.SelectedItems[0].Remove();
                if (listView1.SelectedIndices == null)
                {
                    MessageBox.Show("亲你没有选中行");
                }
            }
            //群删
            if (true)
            {
                listView1.Items.Clear();
            }
            //退出
            if (true)
            {
                Reusing.TuiChu();
            }
            //清除会员
            if (true)
            {
                
            }
            //切换用户
            if (true)
            {
            Login Register = new Login();
            Register.ShowDialog();
            this.Hide();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Login Register = new Login();
            Register.ShowDialog();
            this.Hide();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //数量的
            int a = -1;
            if (listView1.SelectedIndices != null && listView1.SelectedIndices.Count > 0)
            {   a = int.Parse(listView1.SelectedItems[0].SubItems[6].Text);
                string abc = listView1.SelectedItems[0].SubItems[0].Text;
                --a;
                string sqlstr = "select UnitPrice*Discount from CommodityList where ProductID='" + abc + "'";//查询我商品的价格。
                dr = DBHelper.DateReader(sqlstr);
                double zhao2 = 0;
                while (dr.Read())
                {
                    zhao2 = Convert.ToDouble(dr[0].ToString());
                }
                dr.Close();
                listView1.SelectedItems[0].SubItems[6].Text = a.ToString();
                double c = a * zhao2;
                listView1.SelectedItems[0].SubItems[7].Text = c.ToString();
            }
           if (a == 0)
            {
                listView1.SelectedItems[0].Remove();
            }
           if (listView1.SelectedIndices == null || listView1.SelectedIndices.Count < 0)
            {
                MessageBox.Show("亲你没有选中行");
            }
        }
    }
}
